package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    //属性注入
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     * */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category>  page =categoryMapper.pageQuery(categoryPageQueryDTO);
        //封装分页结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增分类
     * @param categoryDTO
     * */
    @Override
    public void save(CategoryDTO categoryDTO) {
        //对象属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //设置停售
        category.setStatus(StatusConstant.DISABLE);
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //设置创建人
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.insert(category);
        //清理当前线程的id
        BaseContext.removeCurrentId();

    }

    /**
     * 分类状态变更
     * @param status
     * @param id
     * */
    @Override
    public void startOrStop(Integer status, Long id) {
        //对status判断
        if(status == null || !(status == 0 || status == 1)){
            throw new BaseException("状态值错误");
        }
        //创建条件构造器
        Category category =Category.builder()
                .id(id)
                .status(status)
                .build();
        //更新数据库
        categoryMapper.update(category);
        //清理当前线程的id
        BaseContext.removeCurrentId();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //对象属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
       /* //设置修改人
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());*/
        //设置停售
        category.setStatus(StatusConstant.DISABLE);
        //更新数据库
        categoryMapper.update(category);
    }

    /**
     * 删除分类
     * @param id
     * */
    @Override
    public void deleteById(Long id) {
        //删除数据库中数据
        //判断当前分类是否关联了菜品
        int count1 = dishMapper.countByCategoryId(id);
        //判断当前分类是否关联了套餐
        int count2 = setmealMapper.countByCategoryId(id);
        if(count1 > 0){
            //当前分类关联了菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if(count2 > 0){
            //当前分类关联了套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }

     /**
     * 分类列表查询
     * @param type
     * @return
     * */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }


}
